/*******************************************************************************
 ** Government Sanctioned Espionage RPG Server                                **
 ** http://www.government-sanctioned.us/                                      **
 **===========================================================================**
 ** Name: script.cpp                                                          **
 ** Description:                         **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 ** The contents of this file are subject to the terms of the                 **
 ** Common Development and Distribution License, Version 1.0 only             **
 ** (the "License").  You may not use this file except in compliance          **
 ** with the License.                                                         **
 **                                                                           **
 ** You can find a copy of the license in the LICENSE file within             **
 ** this distribution or at $WIKI/display/GOVT/License-software.              **
 ** Software distributed under the License is distributed on an "AS IS"       **
 ** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.           **
 ** See the License for the specific language governing permissions           **
 ** and limitations under the License.                                        **
 **                                                                           **
 ** When distributing Covered Code, include this CDDL header in each          **
 ** file and include the License file at $GAME_DIR/LICENSE.                   **
 ** If applicable, add the following below this header, with the indicated    **
 ** fields enclosed by brackets "[]" replaced with your own identifying       **
 ** information: Portions Copyright [yyyy] [name of copyright owner]          **
 **                                                                           **
 ** Copyright (c) 2009-2012  Barry Gian James <gian@gamingods.net>            **
 ** All rights reserved.                                                      **
 ******************************************************************************/
// $HeadURL$
// Modified $Date$ by $Author$
// $Id$

#include <falcon/falcon.h>
#include <falcon/trace.h>
#include <falcon/modloader.h>
using namespace Falcon;

#include "script.hpp"

namespace Falcon {
namespace Ext {

void
CharByName::invoke(VMContext * ctx, int32 p)
{

}

}	/* namespace Ext */
}	/* namespace Falcon */



FalconApp::FalconApp()
: m_exitValue(0)
{
}

void
FalconApp::launch(const String & script)
{
	VMachine vm;
	vm.setStdEncoding(m_options.io_encoding);

	Stream * fs = Engine::instance()->vfs().openRO(script);
	if (fs == 0)
	{
		vm.textOut()->write("Cant open " + script + "\n");
		return;
	}

	ModSpace * ms = vm.modSpace();
	ModLoader * loader = ms->modLoader();

	loader->setSearchPath(".");
	if (m_options.load_path.size() > 0)
		loader->addSearchPath(m_options.load_path);
	if (!m_options.ignore_syspath)
		loader->addFalconPath();
	if (m_options.ignore_sources)
		loader->useSources(ModLoader::e_us_never);
	else if (m_options.force_recomp)
		loader->useSources(ModLoader::e_us_always);

	if (m_options.save_modules)
	{
		if (m_options.force_recomp)
			loader->savePC(ModLoader::e_save_mandatory);
		else
			loader->savePC(ModLoader::e_save_try);
	} else {
		loader->savePC(ModLoader::e_save_no);
	}

	ModLoader::t_modtype	type = ModLoader::e_mt_none;
	if (m_options.run_only)
		type = ModLoader::e_mt_vmmod;
	else if (m_options.compile_only)
		type = ModLoader::e_mt_source;

	Module * module = loader->loadFile(script, type);
	module->setMain();

	ms->add(Engine::instance()->getCore(), true, false);
	ms->resolve(module, true, true);

	Error * linkerr = ms->link();
	if (linkerr != 0)
		throw linkerr;

	Function * fmain = module->getMainFunction();
	if (fmain != 0)
	{
		vm.currentContext()->call(fmain,0);
		ms->readyVM(vm.currentContext());
		vm.run();
	}
}


